<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$subject_id = mysql_real_escape_string(strip_tags($_GET['sid']));
$course_id = mysql_real_escape_string(strip_tags($_GET['id']));

$get_subject_data = mysql_query("SELECT * FROM course_subjects WHERE id = $subject_id");
$subject_name = mysql_result($get_subject_data,0,'subject_title');
$abbreviation = mysql_result($get_subject_data,0,'abbreviation');

$get_course_data = mysql_query("SELECT * FROM courses WHERE id = $course_id");
$course_number = mysql_result($get_course_data,0,'course_number');
$course_name = mysql_result($get_course_data,0,'title');

echo '<p>';
	echo "<a href='../courses.php?id=$subject_id'>&lt;&lt; Back to Courses Portal</a><br />";
echo '</p>';

echo "<h1>$abbreviation $course_number Sections</h1>";
echo '<p>';
//creates link/button for add Course
echo "<a href='add.php?cid=$course_id'>Add Section</a>";
echo '</p>';


//counting variable
$i = 0;

//select statement
$query="SELECT * 
		FROM course_section_mapping 
		JOIN course_sections
		ON course_sections.id=course_section_mapping.course_section_id
		WHERE course_section_mapping.course_id = $course_id 
		ORDER BY course_sections.section_number";
$result=mysql_query($query);

//determine dynamically number of rows read from table
$num_rows=mysql_numrows($result);

$find_insert=mysql_query("SELECT * FROM course_sections");
$insert_point_sec=mysql_numrows($find_insert);

$find_insert=mysql_query("SELECT * FROM course_section_mapping");
$insert_point_csm=mysql_numrows($find_insert);

//add will be done at one past last entry currently in table
$insert_point_sec++;
$insert_point_csm++;

if ($num_rows > 0)
{

	//start table
	echo "<table class=\"postSchedule\">";
		
		//create table row with column ID's
		echo "<tr>";
			echo "<td width='120'><b>Section Number</b></td>";
			echo "<td width='150'><b>Instructor</b></td>";
			echo "<td style='text-align: center' width='100'><b>Room Type</b></td>";
			echo "<td style='text-align: center' width='120'><b>Max Students</b></td>";
			echo "<td style='text-align: center' width='100'><b>Honors</b></td>";
			echo "<td style='text-align: center' width='100'><b>Semester</b></td>";
			echo "<td></td>";
			echo "<td></td>";
			echo "<td></td>";
		echo "</tr>";

	//loop to walk through and print to screen all entries in course_subjects table
	$row_count=0;
	while ($i < $num_rows) 
	{
		$row_count++;

		//store each subject name and abbreviation in a scalar
		$section_id=mysql_result($result,$i,"course_sections.id");
		$csm_id=mysql_result($result,$i,"course_section_mapping.id");
		$section_number=mysql_result($result,$i,"course_sections.section_number");
		$instructor_id=mysql_result($result,$i,"course_sections.instructor_id");
		$room_type_id=mysql_result($result,$i,"course_sections.classroom_type_id");
		$num_seats=mysql_result($result,$i,"course_sections.num_seats");
		$honors=mysql_result($result,$i,"course_sections.honors_program");
		$semester_id=mysql_result($result,$i,"course_sections.semester_id");
		
		$get_instructor=mysql_query("SELECT * FROM instructors WHERE id=$instructor_id");
		$first_name=mysql_result($get_instructor,0,"first_name");
		$last_name=mysql_result($get_instructor,0,"last_name");
		
		$get_semester=mysql_query("SELECT * 
								   FROM seasons 
								   JOIN semesters
								   ON semesters.season_id=seasons.id
								   WHERE semesters.id=$semester_id");

		$semester=mysql_result($get_semester,0,"seasons.season");
		$year=mysql_result($get_semester,0,"semesters.year");
		
		$get_room_type=mysql_query("SELECT * FROM classroom_types WHERE id=$room_type_id");
		$room_type=mysql_result($get_room_type,0,"description");
		
		$formatted_section_number = sprintf("%03d", $section_number);
		
		//generate table row for each subject on each iteration of the loop
		echo "<tr";
		if (($row_count%2) != 0) echo " class=\"zebra_stripe\"";
		echo ">";
			echo "<td>$formatted_section_number</td>";		//section number cell
			echo "<td>$last_name, $first_name</td>";
			echo "<td style='text-align: center'>$room_type</td>";
			echo "<td style='text-align: center'>$num_seats</td>";		
			
			if ($honors)
			{
				echo "<td style='text-align: center'>Yes</td>";
			}
			else
			{
				echo "<td style='text-align: center'>No</td>";
			}
			
			echo "<td style='text-align: center'>$semester $year</td>";
			
			echo "<td>";		//cell containing form for edit link/button
				echo "<a href='edit.php?sec_id=$section_id'>Edit</a>";
			echo "</td>";
			
			echo "<td>";		//cell containing form for delete link/button
				echo "<a href='delete.php?sec_id=$section_id'>Delete</a>";
			echo "</td>";
			
			echo "<td>";		//cell containing form for check crosslist link/button
				echo "<a href='crosslist/?sec_id=$section_id'>Check Crosslisting</a>";
			echo "</td>";
		echo "</tr>";
			
		//increment counting variable
		$i++;
	}

	//end table tag
	echo "</table>";

}
else
{
	echo "<p><b>NO SECTIONS CURRENTLY ASSIGNED TO COURSE</b></p>";
}



?>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>